import global from "../global";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Matching extends cc.Component {

    @property(cc.Node)
    Node_matching:cc.Node = null;       //匹配状态栏

    @property(cc.Label)
    Label_time:cc.Label = null;         //匹配时间

    onLoad(){
        this.Node_matching.active = false;
    }

    /**
     * 点击匹配
     * data  2人局还是3人局
     */
    matching(obj:any,data:number){
        if(!global.is_checkVersion){
            global.persist.tip("更新下版本，兄弟")
            return
        }
        if(global.is_dropLine){
            global.persist.tip("网络断开了，退游吧")
            return
        }
        global.is_replay = false;
        this.Node_matching.active = true;
        this.time = 0;
        this.timer();
        this.schedule(this.timer,1);
        global.send({other:"matching",type:data});
    }

    /**
     * 取消匹配
     * is_server 是否是匹配超时
     */
    cancel(){
        this.Node_matching.active = false;
        this.unschedule(this.timer);
        global.send({other:"matching_cancel"});
    }

    /**
     * 计时器
     */
    time:number = 0;
    timer(){
        let m = this.check(Math.floor(this.time/60));
        let s = this.check(this.time%60);
        this.Label_time.string = m+":"+s;
        this.time++;
    }

    //检查时间格式
    check(i:number):string{  
        let j:string;
        j = String(i);
        if (i<10) {
            j="0" + i;
        }
        return j;   
    }
}
